package test.yzx.pproject.act1;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.jaredrummler.android.processes.AndroidProcesses;
import com.jaredrummler.android.processes.models.AndroidAppProcess;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import test.yzx.pproject.R;


public class Process7_0Activity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scroll_tv);



    }





    private String getProcPathList(){
        return exec("ls -al /proc");
    }

    private java.lang.Process process;

    private String exec(String cmd){
        try{
            process = Runtime.getRuntime().exec(cmd);
            process.waitFor();
            InputStream in = process.getInputStream();
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            return new String(buffer);
        }catch (Exception e){
            Log.e("------>>", "", e);
            return "";
        }
    }

    @Override
    protected void onDestroy() {
        if(process != null)
            process.destroy();
        super.onDestroy();
    }




    public static List<ActivityManager.RunningAppProcessInfo> getRunningAppProcessInfo7_0(Context ctx) {
        ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServiceList = am.getRunningServices(Integer.MAX_VALUE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = new ArrayList<>();
        for (ActivityManager.RunningServiceInfo item : runningServiceList) {
            ActivityManager.RunningAppProcessInfo info = new ActivityManager.RunningAppProcessInfo(item.process, item.pid, new String[]{item.clientPackage});
            appProcessInfos.add(info);
        }
        return appProcessInfos;
    }



    public static List<ActivityManager.RunningAppProcessInfo> getRunningAppProcessInfo(Context ctx) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            List<AndroidAppProcess> processes = AndroidProcesses.getRunningAppProcesses();
            if(processes!=null){
                List<ActivityManager.RunningAppProcessInfo> appProcessInfos = new ArrayList<>();
                for(AndroidAppProcess appProcess:processes){
                    if(appProcess.name.contains("/")){
                        continue;
                    }
                    ActivityManager.RunningAppProcessInfo info = new ActivityManager.RunningAppProcessInfo(appProcess.name, appProcess.pid, new String[]{appProcess.getPackageName()});
                    info.uid = appProcess.uid;

                    appProcessInfos.add(info);
                }
                return appProcessInfos;
            }
        }
        ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        return am.getRunningAppProcesses();
    }

    public static List<String> getInstallAppPackageNameList(){
        ArrayList<String> list = new ArrayList<>();



        return list;
    }

}
